using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;

public partial class admin_customer_edit : SiteAdminBasePage
{
    public int _userId;
    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;
        if (!Page.IsPostBack)
        {
            uccBillingState.DataBind();
            uccBillingCountry.DataBind();
            uccShippingState.DataBind();
            uccShippingCountry.DataBind();
            LoadProfile();
        }
    }

    protected void LoadProfile()
    {
        try
        {
            int userId;
            int.TryParse(Request.QueryString["cuid"].ToString(), out userId);
            _userId = userId;
            if (userId > 0)
            {
                CustomerUser customerUser = CustomerUser.GetCustomerUserByUserId(userId);
                tbxPersonalFirstName.Text = customerUser.Profile.FirstName;
                tbxPersonalLastName.Text = customerUser.Profile.LastName;
                tbxPersonalDateOfBirth.Text = customerUser.Profile.DateOfBirth.ToString("MM/dd/yyyy");
                tbxPersonalEmailAddress.Text = customerUser.Profile.EmailAddress;
                rbtMale.Checked = customerUser.Profile.UserGender == Gender.Male ? true : false;
                rbtFemale.Checked = customerUser.Profile.UserGender == Gender.Female ? true : false;
                tbxContactPhoneNumber.Text = customerUser.Profile.UserTelephoneNumber;
                tbxContactFaxNumber.Text = customerUser.Profile.UserFaxNumber;
                //tbxCompanyName.Text = customerUser.Profile.CompanyName;
                //tbxCompanyPhoneNumber.Text = customerUser.Profile.CompanyTelephoneNumber;
                //tbxCompanyFaxNumber.Text = customerUser.Profile.CompanyFaxNumber;
                tbxBillingFirstName.Text = customerUser.Profile.BillingAddress.FirstName;
                tbxBillingLastName.Text = customerUser.Profile.BillingAddress.LastName;
                tbxBillingPhoneNumber.Text = customerUser.Profile.BillingAddress.TelephoneNumber;
                tbxBillingEmailAddress.Text = customerUser.Profile.BillingAddress.EmailAddress;
                tbxBillingAddressLine1.Text = customerUser.Profile.BillingAddress.AddressLine1;
                tbxBillingAddressLine2.Text = customerUser.Profile.BillingAddress.AddressLine2;
                tbxBillingCity.Text = customerUser.Profile.BillingAddress.City;
                uccBillingState.Items.FindByText(customerUser.Profile.BillingAddress.AddressState).Selected = true;
                uccBillingCountry.Items.FindByText(customerUser.Profile.BillingAddress.AddressCountry).Selected = true;
                tbxBillingZipCode.Text = customerUser.Profile.BillingAddress.ZipCode;
                tbxShippingFirstName.Text = customerUser.Profile.ShippingAddress.FirstName;
                tbxShippingLastName.Text = customerUser.Profile.ShippingAddress.LastName;
                tbxShippingPhoneNumber.Text = customerUser.Profile.ShippingAddress.TelephoneNumber;
                tbxShippingEmailAddress.Text = customerUser.Profile.ShippingAddress.EmailAddress;
                tbxShippingAddressLine1.Text = customerUser.Profile.ShippingAddress.AddressLine1;
                tbxShippingAddressLine2.Text = customerUser.Profile.ShippingAddress.AddressLine2;
                tbxShippingCity.Text = customerUser.Profile.ShippingAddress.City;
                uccShippingState.Items.FindByText(customerUser.Profile.ShippingAddress.AddressState).Selected = true;
                uccShippingCountry.Items.FindByText(customerUser.Profile.ShippingAddress.AddressCountry).Selected = true;
                tbxShippingZipCode.Text = customerUser.Profile.ShippingAddress.ZipCode;
                tbxAdminComment.Text = customerUser.Profile.AdminComment;
                chkActiveUser.Checked=customerUser.IsActive;
                txtPassword.Text = customerUser.LoginPassword;
            }
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to load your account information.");
            uccResponseMessage.Visible = true;
        }
    }


    protected void Create_Click(object sender, EventArgs e)
    {
        try
        {
            this.Page.Validate();

            if (IsValid)
            {
                int userId;
                int.TryParse(Request.QueryString["cuid"].ToString(), out userId);
                _userId = userId;
                CustomerUser customerUser = CustomerUser.GetCustomerUserByUserId(userId);
                if (customerUser != null)
                {
                    customerUser.Profile.FirstName = tbxPersonalFirstName.Text;
                    customerUser.Profile.LastName = tbxPersonalLastName.Text;
                    DateTime dateOfBirth;
                    if (!DateTime.TryParse(tbxPersonalDateOfBirth.Text, out dateOfBirth))
                    {
                        uccResponseMessage.SetFailMessage("Incorrect date.");
                        uccResponseMessage.Visible = true;
                        return;
                    }
                    customerUser.Profile.DateOfBirth = dateOfBirth;
                    customerUser.Profile.EmailAddress = tbxPersonalEmailAddress.Text;
                    Gender userGender;
                    if (rbtMale.Checked)
                    {
                        userGender = Gender.Male;
                    }
                    else
                    {
                        userGender = Gender.Female;
                    }
                    customerUser.IsActive=chkActiveUser.Checked;
                    customerUser.Profile.UserGender = userGender;
                    customerUser.Profile.UserTelephoneNumber = tbxContactPhoneNumber.Text;
                    customerUser.Profile.UserFaxNumber = tbxContactFaxNumber.Text;
                    //customerUser.Profile.CompanyName = tbxCompanyName.Text;
                    //customerUser.Profile.CompanyTelephoneNumber = tbxCompanyPhoneNumber.Text;
                    //customerUser.Profile.CompanyFaxNumber = tbxCompanyFaxNumber.Text;
                    customerUser.Profile.BillingAddress = new AutoGear.BLL.Address(tbxBillingFirstName.Text, tbxBillingLastName.Text, tbxBillingAddressLine1.Text, tbxBillingAddressLine2.Text, tbxBillingCity.Text, uccBillingState.SelectedItem.Text, uccBillingCountry.SelectedItem.Text, tbxBillingZipCode.Text, tbxBillingPhoneNumber.Text, string.Empty, tbxBillingEmailAddress.Text);
                    customerUser.Profile.ShippingAddress = new AutoGear.BLL.Address(tbxShippingFirstName.Text, tbxShippingLastName.Text, tbxShippingAddressLine1.Text, tbxShippingAddressLine2.Text, tbxShippingCity.Text, uccShippingState.SelectedItem.Text, uccShippingCountry.SelectedItem.Text, tbxShippingZipCode.Text, tbxShippingPhoneNumber.Text, string.Empty, tbxShippingEmailAddress.Text);
                    customerUser.Profile.IsActive = true;
                    customerUser.Profile.AdminComment = tbxAdminComment.Text;
                    customerUser.Update();

                    uccResponseMessage.SetSuccessMessage("Updated successfully.");
                    uccResponseMessage.Visible = true;
                }

            }
        }
        catch
        {
            uccResponseMessage.SetFailMessage("Failed to modify your account information.");
            uccResponseMessage.Visible = true;
        }
    }
}
